package cn.y.operation.application.core.service;

import cn.y.operation.application.core.entity.SysUser;
import cn.y.operation.application.core.mapper.ISysUserMapper;
import cn.y.operation.framework.enums.UserLoginTypeEnum;
import cn.y.operation.framework.utils.encryption.RegexUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: SysUserService
 * @author: 叶子豪
 * @create: 2021-02-24 17:59
 */
@Service
public class ISysUserService {

    @Resource
    public ISysUserMapper ISysUserMapper;

    /**
     * 根据用户名和密码查找登录的用户
     */
    public SysUser loginUser(String username) {
        if (StringUtils.isBlank(username)) {
            return null;
        }
        String loginType;
        if (RegexUtils.isMobile(username)) {
            loginType = UserLoginTypeEnum.MOBILE.getInfo();
        } else if (RegexUtils.isEmail(username)) {
            loginType = UserLoginTypeEnum.EMAIL.getInfo();
        } else {
            loginType = UserLoginTypeEnum.USERNAME.getInfo();
        }
        Map<String, Object> map = new HashMap<>();
        map.put(loginType, username);
        return ISysUserMapper.loginUser(map);
    }
}
